package io.gitee.kenewstar.dbmq.util;

/**
 * @author kenewstar
 */
public class MySqlTableSql {

    public static final String TABLE_CONSUMER_REGISTER = "mq_consumer_register";

    private StringBuilder consumerRegisterSql;

    private MySqlTableSql() {
        this.createConsumerRegisterSql();
    }

    public static MySqlTableSql build() {
        return new MySqlTableSql();
    }

    private void createConsumerRegisterSql() {
        this.consumerRegisterSql = new StringBuilder();
        this.consumerRegisterSql.append("CREATE TABLE IF NOT EXISTS `" + TABLE_CONSUMER_REGISTER + "` (")
                .append(" `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', ")
                .append(" `topic` varchar(100) NOT NULL COMMENT '主题，对应实际消费队列表', ")
                .append(" `ip` varchar(20) DEFAULT NULL COMMENT '注册IP', ")
                .append(" `uuid` varchar(36) NOT NULL COMMENT '随机实例码UUID', ")
                .append(" `consume_count` bigint(20) NOT NULL DEFAULT '0' COMMENT '消费数量', ")
                .append(" `heartbeat_time` datetime NOT NULL COMMENT '心跳时间', ")
                .append(" `status` varchar(10) NOT NULL DEFAULT 'up' COMMENT '消费者状态,up/down', ")
                .append(" `version` bigint(20) NOT NULL DEFAULT '1' COMMENT '版本号', ")
                .append("  PRIMARY KEY (`id`) ")
                .append(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
    }

    public String getConsumerRegisterSql() {
        return consumerRegisterSql.toString();
    }

    public String createQueueTableSql(String topic) {
        return "CREATE TABLE IF NOT EXISTS `" + topic + "` (" +
                " `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID', " +
                " `message_body` json NOT NULL COMMENT '消息体', " +
                " `consume_flag` int(10) NOT NULL DEFAULT '0' COMMENT '是否消费', " +
                " `consumer_tag` int(10) NOT NULL COMMENT '消费者标识', " +
                " `creation_date` datetime NOT NULL COMMENT '创建时间', " +
                "  PRIMARY KEY (`id`), " +
                "  KEY `mq_unx1` (`consume_flag`,`consumer_tag`) USING BTREE " +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    }



}
